var loadClock = "";

var loadingImage = "../images/common/loading.gif";

function errh(msg, exc) {
    alert(msg);
}

dwr.engine.setErrorHandler(errh);

dwr.engine.setPreHook(function () {
    // var map = new Map();
    // map.set("Authorization", sessionStorage.getItem("Authorization"));
    var headers = [{
        key: "Authorization",
        value: sessionStorage.getItem("Authorization")
    }];
    dwr.engine.setHeaders(headers);
    // dwr.engine.batch.req.setRequestHeader("Authorization", sessionStorage.getItem("Authorization"));
    // alert(dwr.engine.headers["Authorization"]);
    // dwr.engine.batch.req
    loading(null);
});

function loading(message) {
    var disabledImageZone = document.getElementById("disabledImageZone");
    if (!disabledImageZone) {
        disabledImageZone = document.createElement("div");
        disabledImageZone.setAttribute("id", "disabledImageZone");
        disabledImageZone.style.position = "absolute";
        disabledImageZone.style.zIndex = "1000000";
        disabledImageZone.style.width = "100%";
        disabledImageZone.style.height = screen.height;
        disabledImageZone.style.left = "0px";
        disabledImageZone.style.top = "0px";
        disabledImageZone.style.backgroundColor = "#C9C9C9";
        disabledImageZone.style.textAlign = "center";
        disabledImageZone.style.verticalAlign = "middle";
        disabledImageZone.style.opacity = 0.5;
        if (message) {
            disabledImageZone.innerHTML = "<label id='alertDiv' style='color: #C81B0A; font-weight: bold; vertical-align: middle; position: relative; top: 45%; transform: translateY(-50%);'>" + message + "</label>";
            loadClock = setInterval(hiddenMessage, 100);
        } else {
            disabledImageZone.innerHTML = "<img id='alertDiv' src='" + loadingImage + "' style='vertical-align: middle; position: relative; top: 49%; transform: translateY(-50%);'>";
        }
        document.body.appendChild(disabledImageZone);
        dwr.util._disableZoneUseCount = 1;
    } else {
        document.getElementById("disabledImageZone").style.display = "block";
        disabledImageZone.style.opacity = 0.5;
        if (message) {
            disabledImageZone.innerHTML = "<label id='alertDiv' style='color: #C81B0A; font-weight: bold; vertical-align: middle; position: relative; top: 45%; transform: translateY(-50%);'>" + message + "</label>";
            loadClock = setInterval(hiddenMessage, 100);
        } else {
            disabledImageZone.innerHTML = "<img id='alertDiv' src='" + loadingImage + "' style='vertical-align: middle; position: relative; top: 49%; transform: translateY(-50%);'>";
        }
        dwr.util._disableZoneUseCount = dwr.util._disableZoneUseCount + 1;
    }
    disabledImageZone.style.marginTop = document.documentElement.scrollTop + "px";
}

function loaded() {
    dwr.util._disableZoneUseCount = dwr.util._disableZoneUseCount - 1;
    if (dwr.util._disableZoneUseCount == 0) {
        document.getElementById("disabledImageZone").style.display = "none";
    }
}

dwr.engine.setPostHook(function () {
    loaded();
});

function hiddenMessage() {
    document.getElementById("disabledImageZone").style.opacity = document.getElementById("disabledImageZone").style.opacity - 0.05;
    if (document.getElementById("disabledImageZone").style.opacity < 0.1) {
        document.getElementById("disabledImageZone").style.opacity = 0;
        document.getElementById("disabledImageZone").style.display = "none";
        clearInterval(loadClock);
        dwr.util._disableZoneUseCount = dwr.util._disableZoneUseCount - 1;
    }
}

//获取元素的纵坐标
function getTop(e) {
    var offset = e.offsetTop;
    if (e.offsetParent != null) {
        offset += getTop(e.offsetParent);
    }
    return offset;
}

//获取元素的横坐标
function getLeft(e) {
    var offset = e.offsetLeft;
    if (e.offsetParent != null) {
        offset += getLeft(e.offsetParent);
    }
    return offset;
}

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function validate(form) {
    var elements = form.elements;
    var element;
    var name;
    var value;
    var message;
    for (var i = 0; i < elements.length; i++) {
        element = elements[i];
        if (element.required) {
            name = element.name;
            value = dwr.util.getValue(name);
            if (value == null || value.length == 0 || !value) {
                element.focus();
                message = "请填写" + element.placeholder;
                alert(message);
                return false;
            }
        }
    }
    return true;
}

function setModelToForm(model) {
    for (var field in model) {
        if (typeof (model[field]) == "string" || typeof (model[field]) == "number" || typeof (model[field]) == "boolean") {
            if ($(field) != null) {
                dwr.util.setValue(field, model[field]);
            }
        } else {
            setModelToForm(model[field]);
        }
    }
}
